/*
*	mit.e - Various macros used with mit
*	Version 1
*
*	This source file is the source of a compiled macro that runs under the Epsilon (see note) text editor.
*
*	Incorporating the macro into Epsilon:
*	-------------------------------------
*	
*	Run the Epsilon macro compiler at a dos prompt to compile this file
*	to 'mgtrans.b'.
*		'\epsilon\bin\eel -ic:\eps700\include mit.e'
*		'c:\eps\bin\eel -ic:\eps\include mit.e'
*
*	Then start epsilon and run the epsilon command 'load-bytes' supplying 'mgtrans.b' (without quotes) to load the macro into the text
*	editor.  Run the command 'write-state' so that the next time epsilon is loaded the load-bytes command will not need to be run.
*
*   Note: "Epsilon" is a registered trademark licensed to Lugaru Software, Ltd.  EEL" and "Lugaru" are trademarks of Lugaru Software,
*	"Ltd. 
*
*	Using the macros:
*	----------------
*	MSH Forms macros:
*   mit_replace_first_rtfFieldsLeavingBookmark()
*	mit_replace_rtfFieldsLeavingBookmarks()		
*	mit_remove_pntab()							
*	mit_remove_matchingCurlyBracePairs()		
*	mit_prep_msh_rtf()							
*	mit_isolateBkmkOnAnyLine()					Used with bookmarked RTF files.  Processing by magic requires the bkmk expressions
*												exist by themselves on their own lines.
*												
*	mit_split_back_to_back_MG()					
*	mit_remove_controls_from_datafields()		
*   mit_insert_controls_in_datafields()			
*	mit_mark_omf_htm_delete_area()				
*	mitHtmlTableToPipe()						
*	
*	Created:
*		10/10/2001 Ira Gershenhorn 
*	
*	Updated:
*	2006-0117 IG pcTimeClock_detail_format removed. Created new macro file to hold it.
*	2004-0424 IG pcTimeClock_detail_format updated.
*	2004-0424 IG Add ptreeFixup command
*		10/10/2001 IG No updates
*        
*/
#include "eel.h"

/* An RTF subjected to this macro can still be reimported back into
 * word.  
 */

/*Replace {\field{\*\fldinst {\*\bkmkstart Sex}~ FORMCHECKBOX  {{\*\datafield 65040000140000000353657800000000000000000000000000}}}{\fldrslt }{\*\bkmkend Sex}}
 leaving  {\*\bkmkstart Sex}~{\*\bkmkend Sex}~

 Replace: {\field{\*\fldinst {\*\bkmkstart N1_lCD9CM} FORMTEXT {{\*\datafield 0000000000000000094e315f6c434439434d00000000000000000000000000}}}{\fldrslt {\lang1024 {\enspace  }{\enspace  }{\enspace  }{\enspace  }{\enspace  }}}}{\*\bkmkend N1_lCD9CM}
 leaving:  {\*\bkmkstart N1_lCD9CM}~{\*\bkmkend N1_lCD9CM}~
*/
command mit_replace_first_rtfFieldsLeavingBookmark()
{
	point=0; 
	if (search(1,"FORMCHECKBOX")){
		search(0,"}");
		point += 1;
		stuff("~");
		mark=point;
		search(1,"bkmkend"); search(1,"}");	stuff("~"); search(1,"}");
		delete(point-1,point);//remove the extra brace
		search(0,"{");        //beginning of bkmkend
		delete(mark,point);   //datafield is now gone.
		search(0,"{");        //beginning of bkmkstart
		mark=point;
		search(0,"{");
		search(0,"{");
		delete(mark,point);//datafield is now gone.
	}
	point=0; 
	if (search(1,"FORMTEXT")){
		search(0,"}");
		point += 1;
		stuff("~");
		mark=point;
		search(1,"bkmkend"); search(1,"}");	stuff("~");
		search(0,"{");//beginning of bkmkend
		delete(mark,point);//datafield is now gone.
		search(0,"{");//beginning of bkmkstart
		mark=point;
		search(0,"{");
		search(0,"{");
		delete(mark,point);//datafield is now gone.

	}
}
command mit_replace_rtfFieldsLeavingBookmarks()
{
	point=0; 
	while (search(1,"FORMCHECKBOX")){
		search(0,"}");
		point += 1;
		stuff("~");
		mark=point;
		search(1,"bkmkend"); search(1,"}");	stuff("~"); search(1,"}");
		delete(point-1,point);//remove the extra brace
		search(0,"{");        //beginning of bkmkend
		delete(mark,point);   //datafield is now gone.
		search(0,"{");        //beginning of bkmkstart
		mark=point;
		search(0,"{");
		search(0,"{");
		delete(mark,point);//datafield is now gone.
	}
	point=0; 
	while (search(1,"FORMTEXT")){
		search(0,"}");
		point += 1;
		stuff("~");
		mark=point;
		search(1,"bkmkend"); search(1,"}");	stuff("~");
		search(0,"{");//beginning of bkmkend
		delete(mark,point);//datafield is now gone.
		search(0,"{");//beginning of bkmkstart
		mark=point;
		search(0,"{");
		search(0,"{");
		delete(mark,point);//datafield is now gone.

	}
}
command mit_remove_pntab()
{
	point=0; string_replace( "<Min>%{\pntxt.+%}", "", REGEX );
}
command mit_remove_matchingCurlyBracePairs()
{
	point=0; string_replace( "<Min>%{[^{]+%}", "", REGEX );
}

command mit_prep_msh_rtf()
{
//  Don't play with the tildes.  They are also to preserve formatting.
//  Tildes are removed by Magic from a temporary copy prior to the merge.	
//  They may appear separately from bookmarks
//	point = 0;	string_replace( "~", "", 0 );// the tildes were only to keep the bookmarks separated.
//	point = 0;	string_replace( "{\\\*\\bkmk", "\n{\\\*\\bkmk", 0 );// ensure all bookmarks start a new line.
	point = 0;	string_replace( "%{\\%*\\bkmk[^}]+%}", "\n#0\n", REGEX );// ensure all bookmarks exist on their own line
	point = 0;	string_replace( "{\header ", "\n{\header ", 0 );// ensure all headers start a new line.
	point = 0;	string_replace( "{\footer ", "\n{\footer ", 0 );// ensure all footers start a new line.
	point = 0;	string_replace( "\par }}", "\par }}\n", 0 );// header/footer endings
/*	point = 0;	string_replace( "\\cell([^x])", "\n\\cell\n#1", REGEX );//ensure all cell instructions are alone on a line */
	point = 0;	string_replace( " ?\\cell![^x] ?", "\n\\cell\n", REGEX );//ensure all cell instructions are alone on a line  -- use lookahead so we don't have to restore the extra characters we searched for.
	point = 0;	string_replace( "\cell\n ", "\cell\n", 0 );//2004-0614 delete blank space following a \cell<nl> that we introduced by putting cells on their own line.
	point = 0;	string_replace( "\cell\n ", "\cell\n", 0 );//2004-0614 delete blank space following a \cell<nl> that we introduced by putting cells on their own line.
//	point = 0;	mit_isolateBkmkOnAnyLine();
	point = 0;
	while (re_search(1,"bkmk[^}]+$")){
		delete(point,point+1);//remove the newline
	}
	point = 0;	string_replace( "^ +\}", "}", REGEX );// get rid of white space
	point = 0;	string_replace( "\n\n", "\n", 0 );// get rid of white space I created.
	point = 0;	string_replace( "\n\n", "\n", 0 );// get rid of white space I created.
	point = 0;	string_replace( "\n\n", "\n", 0 );// get rid of white space I created.
	point = 0;	string_replace( "\n\n", "\n", 0 );// get rid of white space I created.
	point = 0;	string_replace( "\n \n", "\n", 0 );// get rid of white space I created.
	//Escape with % for epsilon, escape \ with \ because its in a string.
	point = 0;	string_replace( "%{\\%*\\bkmkstart MG_(.+)%}", "#0<!$MG_#1>", REGEX );

	point = 0;	string_replace( "%{\\%*\\bkmkstart MGR_(.+)%}", "#0<!$MGIF_#1><!$MGREPEAT>", REGEX );
	point = 0;	string_replace( "%{\\%*\\bkmkend MGR_(.+)%}", "<!$MGENDREPEAT><!$MGENDIF>#0", REGEX );

	point = 0;	string_replace( "%{\\%*\\bkmkstart MGIF_(.+)%}", "#0<!$MGIF_#1>", REGEX );
	point = 0;	string_replace( "%{\\%*\\bkmkend MGIF_(.+)%}", "<!$MGENDIF>#0", REGEX );
	point = 0;  string_replace( "\n~","~\n", 0 ); //2004-0614: swap tildes and newlines .

/*	point = 0;	string_replace( "\\cell <!$", "\\cell \n<!$", 0 );// insert Newline into "\cell <!$" construct to eliminate back-to-back replacement entries.
	point = 0;	string_replace( "{\\v ", "\n{\\v ", 0 );// make the hidden instruction occur at the beginning of the line so that only one MG_xxx directive occurs per line.
	point = 0;	string_replace( "{\\v\\", "\n{\\v\\", 0 );// make the hidden instruction occur at the beginning of the line so that only one MG_xxx directive occurs per line.
	point = 0;	string_replace( "{\\\*\\bkmkstart ", "\n{\\\*\\bkmkstart ", 0 );// readability
	point = 0;	string_replace( "\\\*\\formfield", "", 0 );// so checkboxes can be checked.

	point = 0;	mit_split_back_to_back_MG();
*/ //	point = 0;	mit_insert_controls_in_datafields();
}
//	point = 0;	string_replace( "%{\\%*\\bkmk[^}]+%}", "\n#0\n", REGEX );// ensure all bookmarks exist on their own line

command mit_rtfpostproc(){
	point = 0;	string_replace( "%{\\%*\\bkmk(start|end) M(G|GR|GIF)_([^}]+)%}", "", REGEX );// remove our bookmarks
	point = 0;	string_replace( "~", "", 0 );//remove the tildes
	point = 0;	string_replace( "%{\\fs2 <NEWLINE>+\\par }", "", REGEX );//remove pseudo table breaks
}

command mit_isolateBkmkOnAnyLine()
{
	while( search( 1, "\\\*\\bkmkstart" )){
		search( 1, "}" );
		stuff("~\n");
	}
	point = 0;
	while( search( 1, "\\\*\\bkmkend" )){
		search( 1, "}" );
		stuff("~\n");
	}
}
/*
command mit_split_back_to_back_MG()
{
	string_replace( "\>\<\!\$MG", "\>\n\<\!\$MG", 0 );
}
command mit_remove_controls_from_datafields()
{
	point=0; string_replace( "<Min>0%<.+%>", "00", REGEX );
}
command mit_insert_controls_in_datafields()
{   
	char ref[100];

	while( search( 1, "\\bkmkstart" )){
		point +=1;
		mark = point;
		re_search(1,"%}"); //forward_word didn't work here
		grab( mark, point-1, ref);
		if( search( 1, "datafield" )){
			re_search(1,"%}"); //forward_word didn't work here
			point -= 23;
			delete( point-1, point );
			stuff("<!$MG_");
			stuff(ref);
			stuff(">");
		}
	}
}
*/
command mit_mark_omf_htm_delete_area()
{
	if( search( 1, "function _InitVars" )){
		beginning_of_line();
		mark = point;
		search( 1, "// -->" );
		Beginning_Of_Line();
	}
}
command mitHtmlTableToPipe()
{
	point=0; string_replace( "<Min>%<font.+%>", "", REGEX );
	point=0; string_replace( "<Min>%<td.+%>", "|", REGEX );
	point=0; string_replace( "<Min>%<[pb/].*%>", "", REGEX );
	point=0; string_replace( "<Newline>", "", REGEX );
	point=0; string_replace( "<tr>", "", 0 );
	point=0; string_replace( "<Min>%<td.*%>", "", REGEX );
	point=0; string_replace( "&amp;", "&", 0 );
	point=0; string_replace( "^%|", "", REGEX );
}
command mit_test(){
	string_replace( "%{\\%*\\bkmkstart MG_(.+)%}", "#0<!$MG_#1>", REGEX );//
	//%{%\%*%\bkmkstart MG_([^}]+)%}~

}
command mit_test2(){
	string_replace( "\\b", "#0<xx>", REGEX );//

//string_replace( "MG_(.+)\}\~", "#0<!$MG_#1>", REGEX );//
	//%{%\%*%\bkmkstart MG_([^}]+)%}~

}
command ptreeFixup(){
	string_replace( "\n                NTVDM", "	NTVDM", 0 );//
	string_replace( "\n              CMD", "	CMD", 0 );//
}
